package com.seari.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.seari.bean.RtSwitch;
import com.seari.main.StartUpClass;
import com.seari.mapper.RtSwitchMapper;
import com.seari.pojo.MessageBean;
import com.seari.service.RtSwitchService;
import com.seari.utils.StringHelper;

/**
 * Created by 98383 on 2019/5/6.
 */
@Service
public class RtSwitchServiceImpl implements RtSwitchService{
    @Resource
    RtSwitchMapper rtSwitchMapper;
    @Override
    public MessageBean getData(String devId) {
        MessageBean messageBean = new MessageBean();
        Map<String,Object> resultMap = new HashMap<>();
        List<Map> typeList = new ArrayList<Map>();
        DecimalFormat df = new DecimalFormat("0.0");
        if(StartUpClass.switchPerformance.equals("test"))
        {
            RtSwitch rtSwitch=rtSwitchMapper.selectByPrimaryKey(devId);
            messageBean.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
            resultMap.put("CPU", rtSwitch.getDevCpu());
            resultMap.put("internalStorage", (Integer.parseInt(rtSwitch.getDevAllocatedMemory())+Integer.parseInt(rtSwitch.getDevFreeMemory()))/1024*100);
            resultMap.put("flashMemory",df.format(Double.parseDouble(rtSwitch.getDevAllocatedMemory())/(Double.parseDouble(rtSwitch.getDevAllocatedMemory())+Double.parseDouble(rtSwitch.getDevFreeMemory()))*100));
            Set<Map.Entry<String, Object>> entrySet = resultMap.entrySet();
            Iterator<Map.Entry<String, Object>> it = entrySet.iterator();
            while(it.hasNext()){
                Map<String,Object> item=  new HashMap<String, Object>();
                Map.Entry m = (Map.Entry)it.next();
                item.put("device",m.getKey());
                item.put("state",m.getValue());
                typeList.add(item);
            }
            messageBean.setData(typeList);

        }else{
        }
        return messageBean ;
    }
}
